package com.lkwlw.tplms.action;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.lkwlw.tplms.mapper.VehicleInfoMapper;
import com.lkwlw.tplms.model.Pager;
import com.lkwlw.tplms.model.VehicleInfo;
import com.lkwlw.tplms.model.VehicleInfoExample;
import com.lkwlw.tplms.model.VehicleInfoExample.Criteria;
import com.lkwlw.tplms.system.dao.model.Admininfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class VehicleAction extends ActionSupport{

	private static final long serialVersionUID = -3186752495043411984L;
	
	private VehicleInfo vehicle;
	private VehicleInfoMapper vehicleDAO;
	private String productionDate;
	private String intakeDate;
	private String selectIds;
	private List<VehicleInfo> vehicleList;
	private Pager pager;	
	private int page = 1;
	
	public String execute() throws Exception {
		VehicleInfoExample example = new VehicleInfoExample();
		Criteria criteria = example.createCriteria();
		if(vehicle != null){
			if (null != vehicle.getVehicleNo() && !"".equals(vehicle.getVehicleNo())) {
				criteria.andVehicleNoLike("%" + vehicle.getVehicleNo() + "%");
			}
			if (null != vehicle.getLicensePlateNo()&& !"".equals(vehicle.getLicensePlateNo())) {
				criteria.andLicensePlateNoLike("%" + vehicle.getLicensePlateNo() + "%");
			}
			if (null != vehicle.getVehicleType() && !"".equals(vehicle.getVehicleType())) {
				criteria.andVehicleTypeEqualTo(vehicle.getVehicleType());
			}
			if (null != vehicle.getState() && !"".equals(vehicle.getState())) {
				criteria.andStateEqualTo(vehicle.getState());
			}
		}
		example.setOrderByClause("vehicleno asc");
		getPagination(example);
		clearActionParam();
		return SUCCESS;
	}
	public String add() throws Exception {
		clearActionParam();
		return SUCCESS;
	}
	public String edit(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String editId= request.getParameter("editVehicleId");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");		
		vehicle = vehicleDAO.selectByPrimaryKey(editId);
		productionDate = format.format(vehicle.getProductionDate());
		intakeDate = format.format(vehicle.getIntakeDate());
		selectIds = vehicle.getVehicleType();
		selectIds += "," + vehicle.getVehicleBrand();
		selectIds += "," + vehicle.getFuelType();
		selectIds += "," + vehicle.getState();
		return SUCCESS;
	}
	public String save() throws Exception {
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();
		if (session.get("admin") != null) {
			Admininfo admin = (Admininfo) session.get("admin");
			vehicle.setRegistrant(admin.getFullName());
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");		
			vehicle.setProductionDate(format.parse(productionDate));
			vehicle.setIntakeDate(format.parse(intakeDate));
			if(null == selectIds || "".equals(selectIds)){
				vehicleDAO.insertSelective(vehicle);
			}else {
				vehicleDAO.updateByPrimaryKeySelective(vehicle);
			}
			execute();
			return SUCCESS;
		}
		return ERROR;
	}
	private void clearActionParam(){
		selectIds = "";
		productionDate = "";
		intakeDate = "";
	}
	public String delete() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("deleteVehicleIds");
		String[] deleteIds = ids.split(",");
		if (1 < deleteIds.length) {
			vehicleDAO.deleteByPrimaryKeys(deleteIds);
		}else {
			vehicleDAO.deleteByPrimaryKey(ids);
		}		
		execute();
		return SUCCESS;
	}
	
	private void getPagination(VehicleInfoExample example){
		int pageSize=10;
		pager.setCurrentPage(page);
		pager.setPageSize(pageSize);
		
		// 获得总记录数
		int totalSize = vehicleDAO.countByExample(example);
		pager.setTotalCount(totalSize);
		Map<String, Integer> limitParam = new HashMap<String, Integer>();
		limitParam.put("offset", pager.getStart());
		limitParam.put("rows", pager.getPageSize());
		
		vehicleList = null;		
		vehicleList = vehicleDAO.selectByExample(limitParam,example);
		pager.setItems(vehicleList);
		pager.setPaginationModel(getPagerStr(totalSize));		
	}
	
	/**
	 * 组织分页导航栏
	 * @return
	 */
	public String  getPagerStr(int totalSize) {
		
		StringBuilder sb=new StringBuilder();
		
		int pageCount=pager.getTotalPage();//总页数
		int a_total=10;      //分页条中有多少个超链接
		
		sb.append("<table><tr><td align='center'class='wordblue'>共" + pager.getTotalCount()+"条数据&nbsp;&nbsp;页次");
		sb.append(pager.getCurrentPage()+"/" + (totalSize == 0 ? 1 : pageCount) + "页&nbsp;&nbsp;</td>");
		//实际应用中修改上面第三个参数即可

		int a_padding=(int)Math.ceil(a_total/2);  //中间的那个超链接距离边缘链接的间隔a的个数 例如：共11个分页 那么这个就是5

		if(pageCount - pager.getCurrentPage() <= a_padding && pager.getCurrentPage() > a_padding + 1) {
			a_padding=a_total-(pageCount-pager.getCurrentPage());
		}

		int start=pager.getCurrentPage()-a_padding,
		end=start+a_total;
		

		if(pager.getCurrentPage() > 1){
			sb.append("<td class='wordblue'><a href=queryVehicle?page=1>首页</a>&nbsp;&nbsp;</td>");
			sb.append("<td ><div class='prepage wordblue'><a href=queryVehicle?page="+(pager.getCurrentPage()-1)+">上一页</a></div></td>");
		}
		sb.append("<td ><div class='pagelist'>");
		if (totalSize > 0) {
			for (int i = start; i <=end; i++) {
				if(i<=0){
					end+=Math.abs(i);
					i=1;	
				}
				sb.append(" <a href=queryVehicle?page=" + i + " " + ( i == pager.getCurrentPage() ? "style=\"color:red;\"":"") + "> " + i + " </a>");	
				if(i==pageCount){
					break;
				}
			}
		}		
		sb.append("</div></td>");
		if(pager.getCurrentPage()-pageCount<0){
			sb.append("<td><div class='nextpage wordblue'>");
			sb.append("<a href=queryVehicle?page=" + ( pager.getCurrentPage() + 1 ) + ">下一页</a></div></td>");
			sb.append("<td><div class='wordblue'><a href=queryVehicle?page=" + pageCount + ">尾页</a></div></td>");
			}
		sb.append("</tr></table>");
		return sb.toString();
	}
	public VehicleInfo getVehicle() {
		return vehicle;
	}
	public void setVehicle(VehicleInfo vehicle) {
		this.vehicle = vehicle;
	}
	public VehicleInfoMapper getVehicleDAO() {
		return vehicleDAO;
	}
	public void setVehicleDAO(VehicleInfoMapper vehicleDAO) {
		this.vehicleDAO = vehicleDAO;
	}
	public String getProductionDate() {
		return productionDate;
	}
	public void setProductionDate(String productionDate) {
		this.productionDate = productionDate;
	}
	public String getIntakeDate() {
		return intakeDate;
	}
	public void setIntakeDate(String intakeDate) {
		this.intakeDate = intakeDate;
	}
	public String getSelectIds() {
		return selectIds;
	}
	public void setSelectIds(String selectIds) {
		this.selectIds = selectIds;
	}
	public List<VehicleInfo> getVehicleList() {
		return vehicleList;
	}
	public void setVehicleList(List<VehicleInfo> vehicleList) {
		this.vehicleList = vehicleList;
	}
	
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
}
